import { defHttp } from '@/utils/http/axios';
import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel';

import { ErrorMessageMode } from '#/axios';

enum Api {
  Login = '/sys/user/login',
  Logout = '/logout',
  GetUserInfo = '/sys/user/getUserInfo',
  GetPermCode = '/getPermCode',
  TestRetry = '/testRetry',
  getUserByIdApi = '/sys/user/getUserById',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp
    .post<LoginResultModel>(
      {
        url: Api.Login,
        params,
      },
      {
        isTransformResponse: false,
        errorMessageMode: mode,
      },
    )
    .then((response) => {
      console.log('loginApi response:', response); // 添加日志
      return response;
    });
}

export function getUserById(params) {
  return defHttp
    .get(
      {
        url: Api.getUserByIdApi + '?userId=' + params,
      },
      {
        isTransformResponse: false,
      },
    )
    .then((res) => {
      console.log(res,'测试路由')
      return res;
    })
    .catch((err) => {
      console.log(err, '获取路由');
    });
}
/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>(
    { url: Api.GetUserInfo },
    { errorMessageMode: 'none', isTransformResponse: false },
  );
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.get({ url: Api.Logout });
}

export function testRetry() {
  return defHttp.get(
    { url: Api.TestRetry },
    {
      retryRequest: {
        isOpenRetry: true,
        count: 5,
        waitTime: 1000,
      },
    },
  );
}
